﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_GhepBan_Huy : Form
    {
        public Frm_GhepBan_Huy()
        {
            InitializeComponent();
        }
        string BHuy = "";
        private void Frm_GhepBan_Huy_Load(object sender, EventArgs e)
        {
            Load_LV_BHuy();
        }
        public void Load_LV_BHuy()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.MA_BanHuy();
            load_BHuy(tb);
        }
        public void load_BHuy(DataTable tb)
        {
            LV_BHuy.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "5")
                    item.ImageIndex = 1;
                else
                {
                    if (tb.Rows[i][2].ToString() == "2")
                        item.ImageIndex = 2;
                }
                LV_BHuy.Items.Add(item);
            }
        }

        private void LV_BHuy_MouseClick(object sender, MouseEventArgs e)
        {
            BHuy = LV_BHuy.FocusedItem.SubItems[0].Text;
            string st = string.Format("UPDATE Ban SET TinhTrang=5 WHERE TinhTrang=2");
            SqlCommand comd = new SqlCommand(st, Coso.con);
            comd.ExecuteNonQuery();
            string str = string.Format("UPDATE Ban SET TinhTrang=2 WHERE MaBan='{0}'", BHuy);
            SqlCommand com = new SqlCommand(str, Coso.con);
            com.ExecuteNonQuery();
            Load_LV_BHuy();
        }

        private void Frm_GhepBan_Huy_FormClosed(object sender, FormClosedEventArgs e)
        {
            string st = string.Format("UPDATE Ban SET TinhTrang=5 WHERE TinhTrang=2");
            SqlCommand com = new SqlCommand(st, Coso.con);
            com.ExecuteNonQuery();
        }

        private void Btn_huy_Click(object sender, EventArgs e)
        {
            //BHuy = LV_BHuy.FocusedItem.SubItems[0].Text;
            string st = string.Format("UPDATE Ban SET TinhTrang=0,GhepBan=0 WHERE TinhTrang=2 AND MaBan='{0}'", BHuy);
            SqlCommand com = new SqlCommand(st, Coso.con);
            com.ExecuteNonQuery();
            Load_LV_BHuy();
        }
    }
}
